package org.linlinjava.litemall.core.payment.clover.service;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CloverTokenManager {
    public static final boolean ENABLE = false;
//    public static final String  tokenKey = "cloverToken";

    private static ConcurrentHashMap<String, Map<Integer,String>> cacheDataList = new ConcurrentHashMap<>();


    /**
     * 判断缓存中是否有数据
     *
     * @return
     */
    public static boolean hasData(String cardNumber,Integer userId) {

        Map<Integer,String> cardMap = cacheDataList.get(cardNumber);

        if (cardMap == null) {
            return false;
        } else{
            if(cardMap.get(userId) != null){
                return true;
            }else{
                return false;
            }
        }
    }

    /**
     * 缓存token数据
     *
     * @param cloverToken
     */
    public static void loadData(String cardNumber,String cloverToken,Integer userId) {

        Map<Integer,String> cardMap = null;
        if (cacheDataList.get(cardNumber) == null) {
            cardMap = new HashMap<>();
//            cacheDataList.remove(tokenKey);
            cardMap.put(userId,cloverToken);
            cacheDataList.put(cardNumber, cardMap);
        }else{
            cardMap = cacheDataList.get(cardNumber);
            cardMap.put(userId,cloverToken);
        }

    }

    public static String getCloverToken(String cardNumber,Integer userId) {
        if(cacheDataList.get(cardNumber) != null){
            Map<Integer,String> cardMap = cacheDataList.get(cardNumber);
            return cardMap.get(userId) != null ? cardMap.get(userId) : null ;
        }else{
            return  null;
        }
    }



    /**
     * 清除所有缓存
     */
    public static void clearAll() {
        cacheDataList = new ConcurrentHashMap<>();
    }

    /**
     * 清除缓存数据
     */
    public static void clear(String cardNumber,Integer userId) {
        Map<Integer,String> cardMap = cacheDataList.get(cardNumber);
        if (cardMap != null) {
            cardMap.remove(userId);
        }
    }

}
